package com.qdairlines.aop;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.qdairlines.constant.DateTimeConsts;
import com.qdairlines.entity.user.User;
import com.qdairlines.service.log.LogService;

/**
 * 日志管理
 * 
 * @author zhangxu
 * @date 2016年7月26日
 * @company QingDao Airlines
 * @description
 */
@Component
@Aspect
@Order(value = 1)
public class LogDaoAspect {
	private String requestPath = null; // 请求地址
	private String requestIp = null;
	private String workcode = null; // 工号
	private String inputParam = null; // 传入参数
	private String packageName = null; // 包名
	private String methodName = null; // 方法名
	private String time = null; // 当前时间

	@Autowired
	private LogService logService;

	/**
	 * 方法调用后触发 记录结束时间
	 * 
	 * @param joinPoint
	 * @param returnValue
	 */

	@AfterReturning(pointcut = "execution(* com.qdairlines.dao..*.*(..))", returning = "returnValue")
	public void doAfterInServiceLayer(JoinPoint joinPoint, Object returnValue) {
		methodName = joinPoint.getSignature().getName().toLowerCase();
		if (methodName.startsWith("update") || methodName.startsWith("insert")
				|| methodName.startsWith("delete")) {
			packageName = joinPoint.getSignature().getDeclaringTypeName();
			
			//输入参数格式化
			Object[] params = joinPoint.getArgs();
			StringBuffer sb = new StringBuffer();
			if(params !=null && params.length>=1){
				for(Object o : params){
					String temp =  ToStringBuilder.reflectionToString(o,ToStringStyle.DEFAULT_STYLE)
										.replaceAll(",.*?=<null>", "")
										;
					//如果属性为空，则会打印 属性=<null>，对于日志记录来说并不需要，所以通过正则将这个删除
					sb.append(temp);
				}
			}
			inputParam = sb.toString();
			
			time = new DateTime().toString(DateTimeConsts.YYYYMMDDHHMMSS);
			RequestAttributes ra = RequestContextHolder.getRequestAttributes();
			ServletRequestAttributes sra = (ServletRequestAttributes) ra;
			if (sra != null) {
				HttpServletRequest request = sra.getRequest();
				// 获取请求地址
				requestPath = request.getRequestURI();
				System.out.println("@AfterReturning：请求地址为：" + requestPath);
				requestIp = request.getRemoteAddr();
				System.out.println("@AfterReturning：请求IP为：" + requestIp);
				User user = (User) request.getSession().getAttribute(
						"currentUser");
				workcode = user.getWorkcode();
				System.out.println("@AfterReturning：请求人工号：" + workcode);
			}
			// 插入数据库，记录日志
			System.out.println("@AfterReturning：日志记录功能...");
			logService.recordLog(requestPath, requestIp, workcode, inputParam,
					packageName, methodName, time, methodName.substring(0, 6));
		}
	}
}
